<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class User_profiles_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'html', 'language', 'url'));
    }

    function getAllUsers(){
        $query = $this->db->get('users');
        return $query->result();
    }

    function getUserInfos($user_id){
        $sql = "
        SELECT id, user_id, firstname, lastname, country, website, username, email, role_id
        FROM users u, user_profiles up
        WHERE u.id = up.user_id
        AND u.id = ".$user_id;
        $query = $this->db->query($sql);
        return $query->result();
    }

    function get_by_user($user_id) {
        return $this->db->get_where('user_profiles', array('user_id' => $user_id))->row();
    }

    function get_user_fullname($user_id=null) {
        if(!isset($user_id))
            $user_id = $this->tank_auth->get_user_id();

        $this->db->select('concat_ws(" ", up.firstname, up.lastname) as fullname', false)
                 ->from('user_profiles up')
                 ->where('up.user_id', $user_id);

        return $this->db->get()->row()->fullname;
    }

    function save($data, $user_id = null){
        if (!empty($user_id)){
            $this->db->where('user_id', $user_id);
            if (!$this->db->update('user_profiles', $data)){
                return false;
            }
        }
        return true;
    }
}